package com.penghaisoft.microwms.materialhand.web.intowarehouse.controller;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.HandBaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.RawBarcodeDto;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.materialhand.web.intowarehouse.service.IIntoWarehouseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 原材料入库
 * @author luoteng
 * @date 2018年5月28日 上午11:10:17 
 * @version 1.0
 */
@RestController
@RequestMapping("intowh/mat")
public class IntoWarehouseController extends HandBaseController {

    @Autowired
    private IIntoWarehouseService intoWarehouseService;
    
    @Resource
	private StringRedisTemplate stringRedisTemplate;//注入对象
    
    /** 
     * @Title scanBarcode
     * @Description 扫码入库
     * @author luoteng
     * @date 2018年5月29日:上午10:38:00
     * @param rawBarcodeDto
     * @return HandResp
     */
    @PostMapping("scan")
    public HandResp scanBarcode(@RequestBody RawBarcodeDto rawBarcodeDto){
        HandResp resp = new HandResp();
        resp.setCode(Constant.SUCCESS);
        if (rawBarcodeDto.getBarcode() == null || rawBarcodeDto.getBarcode().equals("")){
        	resp.setCode(Constant.FAIL);
        	resp.setMsg("请扫描条码！");
            return resp;
        }
        
        String key = Constant.RAW_INWH_PREFIX + rawBarcodeDto.getBarcode();
        if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前条码正在处理中，请不要重复扫描！");
			return resp;
		}else{
//			是否合格：0-不合格 1-合格
			if(rawBarcodeDto.getIsQualified() != null && rawBarcodeDto.getIsQualified().equals("0")){
				if(rawBarcodeDto.getNotQualifiedReason() == null || rawBarcodeDto.getNotQualifiedReason().equals("")){
					resp.setCode(Constant.FAIL);
					resp.setMsg("请选择不合格原因！");
					return resp;
				}
			}else if(rawBarcodeDto.getIsQualified() != null && rawBarcodeDto.getIsQualified().equals("1")){
				rawBarcodeDto.setNotQualifiedReason(null);
			}else{
				resp.setCode(Constant.FAIL);
				resp.setMsg("参数传递错误！");
				return resp;
			}
			
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			
			TransResult result = intoWarehouseService.scanBarcode(rawBarcodeDto);
	        resp.setCode(result.getCode());
	        resp.setMsg(result.getMsg());
	        resp.setList(result.getList());
	        
	        stringRedisTemplate.delete(key);//删除对象
		}
		
        return resp;
    }
    
    /** 
     * @Title delBarcode
     * @Description 退扫条码
     * @author luoteng
     * @date 2018年5月29日:下午1:29:54
     * @param rawBarcodeDto
     * @return HandResp
     */
    @PostMapping("del")
    public HandResp delBarcode(@RequestBody RawBarcodeDto rawBarcodeDto){
        HandResp resp = new HandResp();
        resp.setCode(Constant.SUCCESS);
        if (rawBarcodeDto.getBarcode() == null || rawBarcodeDto.getBarcode().equals("")){
        	resp.setCode(Constant.FAIL);
        	resp.setMsg("请扫描条码！");
            return resp;
        }
        
        String key = Constant.RAW_INWH_PREFIX + rawBarcodeDto.getBarcode();
        
        if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前条码正在处理中，请不要重复扫描！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			
			TransResult result = intoWarehouseService.delBarcode(rawBarcodeDto);
	        resp.setCode(result.getCode());
	        resp.setMsg(result.getMsg());
	        
	        stringRedisTemplate.delete(key);//删除对象
		}
        
        return resp;
    }
}
